package swu.cx.finalproject

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseExpandableListAdapter
import android.widget.ImageView
import android.widget.ListAdapter
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_middle_contact.*
import java.util.ArrayList

class MiddleContact : AppCompatActivity() {
    val parents = arrayListOf<String>("西荣扛把子","路人甲","路人乙","路人丙")
    val childs = arrayListOf<Array<singItem>>(
        arrayOf(singItem("陈雄",R.mipmap.run1),
            singItem("陈子翀",R.mipmap.run2),
            singItem("刘金豪",R.mipmap.run3)),
        arrayOf(singItem("甲",R.mipmap.run4),
            singItem("乙",R.mipmap.run5),
            singItem("丙",R.mipmap.run6)),
        arrayOf(singItem("风",R.mipmap.run7),
            singItem("和",R.mipmap.run8),
            singItem("日",R.mipmap.run9),
            singItem("历",R.mipmap.run10)),
        arrayOf(singItem("",R.mipmap.run7),
            singItem("和",R.mipmap.run8),
            singItem("日",R.mipmap.run9),
            singItem("历",R.mipmap.run10))
    )
    override fun onBackPressed() {
        startActivity(Intent(this,MainActivity::class.java))
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_middle_contact)
        expandList.setAdapter(MyAdapter(parents,childs,this))
        middle_left.setOnClickListener {
            startActivity(Intent(this,LeftContact::class.java))
        }
        middle_right.setOnClickListener {
            startActivity(Intent(this,RightContact::class.java))
        }
    }
}
class MyAdapter(val groupList: ArrayList<String>, val childLis: ArrayList<Array<singItem>>, val mContext: Context): BaseExpandableListAdapter(){
    override fun getGroupCount(): Int {
        return groupList.size
    }

    override fun getChildrenCount(groupPosition: Int): Int {
        return childLis[groupPosition].size
    }

    override fun getGroup(groupPosition: Int): Any {
        return groupList[groupPosition]
    }

    override fun getChild(groupPosition: Int, childPosition: Int): Any {
        return childLis[groupPosition][childPosition]
    }

    override fun getGroupId(groupPosition: Int): Long {
        return groupPosition.toLong()
    }

    override fun getChildId(groupPosition: Int, childPosition: Int): Long {
        return childPosition.toLong()
    }

    override fun hasStableIds(): Boolean {
        return true
    }

    override fun getGroupView(
        groupPosition: Int,
        isExpanded: Boolean,
        convertView: View?,
        parent: ViewGroup?
    ): View {
        val view= convertView ?: LayoutInflater.from(mContext).inflate(R.layout.parent,parent,false)
        val name: TextView =view.findViewById(R.id.parent)
        name.text=groupList[groupPosition]
        return view
    }

    override fun getChildView(
        groupPosition: Int,
        childPosition: Int,
        isLastChild: Boolean,
        convertView: View?,
        parent: ViewGroup?
    ): View {
        val view=convertView ?: LayoutInflater.from(mContext).inflate(R.layout.child,parent,false)
        val imageView=view.findViewById<ImageView>(R.id.childImage)
        val name=view.findViewById<TextView>(R.id.childName)
        imageView.setImageResource(childLis[groupPosition][childPosition].imgId)
        name.text=childLis[groupPosition][childPosition].name
        return view
    }

    override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean {
        return true
    }
}
data class singItem(val name:String,val imgId:Int)